#ifndef TCPSERVICE_H
#define TCPSERVICE_H
#include "Singleton.hpp"
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include "ringBuffer.hpp"

#define BUFFER_SIZE 1024
#define MAX_EVENTS 65535
#define PORT 12355

class TcpService : public Singleton<TcpService>{
    friend class Singleton<TcpService>;
protected:
    TcpService();
    ~TcpService();
public:
    void start();
    void stop();
    bool readCommand(std::string& data, int& client_fd);
    bool response(std::string&& data, int client_fd);

private:
    // 设置文件描述符为非阻塞模式
    int set_nonblocking(int fd);
    int setRingBuffer();
private:
    bool isRunning;
    int service_fd, epoll_fd;
    int toClientBuffer_fd;
    RingBuffer* toRedisBuffer;  // 1MB buffer
    RingBuffer* toClientBuffer;  // 1MB buffer
};

#endif // TCPSERVICE_H